package com.supeng.PMdepot.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supeng.PMdepot.ItemDecoration.SpaceItemDecoration;
import com.supeng.PMdepot.R;
import com.supeng.PMdepot.adapter.RukuAdapter;
import com.supeng.PMdepot.base.BaseActivity;
import com.supeng.PMdepot.common.Constant;
import com.supeng.PMdepot.entity.RukuBeen;
import com.supeng.PMdepot.entity.RukuInfo;
import com.supeng.PMdepot.entity.ServiceResult;
import com.supeng.PMdepot.net.Api;
import com.supeng.PMdepot.net.NetUtils;
import com.supeng.PMdepot.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class YiKuActivity extends BaseActivity {
    @BindView(R.id.et_kucun)
    EditText etKucun;
    public String kufang;
    public String chanpin;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_cptm)
    TextView tvCptm;
    @BindView(R.id.tv_cpmc)
    TextView tvCpmc;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.vv_blue2)
    View vvBlue2;
    @BindView(R.id.tv_dycw)
    TextView tvDycw;
    @BindView(R.id.tv_ykcw)
    TextView tvYkcw;
    @BindView(R.id.vv_blue3)
    View vvBlue3;
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
    private RukuAdapter homeAdapter;
    private RukuBeen rukuBeen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //弹出键盘滚动问题
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_yiku);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
        initAdapter();
    }

    private void initData() {

        etKucun.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == 0 || actionId == 6) {
                    if (event == null) {
                        if (actionOk(v)) return true;
                    } else {
                        switch (event.getAction()) {
                            case KeyEvent.ACTION_UP:
                                if (actionOk(v)) return true;
                        }
                    }
                }
                return true;

            }
        });


    }

    private boolean actionOk(TextView v) {
        if (!TextUtils.isEmpty(chanpin) && !TextUtils.isEmpty(kufang)) {
            etKucun.setText("");
            ToastUtils.show("请等待上一个条码入库才可以进行下一条码扫描");
            return true;
        }
        String search = v.getText().toString();
        etKucun.setText("");
        if (TextUtils.isEmpty(search)) {
            ToastUtils.show("无效二维码");
            return true;
        }
        if (search.indexOf(Constant.ChanPinKey) == 0) {
            chanpin = search;
            tvCptm.setText(search);
            doSearChCp();
        }else{
            if(TextUtils.isEmpty(chanpin)){
                ToastUtils.show("请先扫描产品二维码");
                return true;
            }
            //库房二维码
            if (search.indexOf(Constant.KuFangkey) == 0) {
                kufang = search.substring(1,search.length());
                tvYkcw.setText(kufang);
            } else {
                ToastUtils.show("无效二维码");
                return true;
            }
        }
        //KeyboardUtils.hideSoftInput(v, getActivity());
        if (!TextUtils.isEmpty(chanpin) && !TextUtils.isEmpty(kufang)) {
            doAddServer(chanpin, kufang);
        }
        return false;
    }

    private void doSearChCp() {
        Api.yksearch(getActivity(), chanpin, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                RukuInfo rukuInfo= (RukuInfo) rspData;
                rukuBeen=rukuInfo.getInfo();
                if(rukuBeen==null){
                    chanpin="";
                    ToastUtils.show("产品条码不存在");
                }else{
                    tvDycw.setText(rukuBeen.getCangwei());
                    tvCpmc.setText(rukuBeen.getChanpin()+rukuBeen.getXinghao());
                    tvCptm.setText(rukuBeen.getCptiaoma());
                }
            }

            @Override
            public void failed(String msg) {

            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, RukuInfo.class);


    }

    private void doAddServer(final String mchanpin, final String mkufang) {
        Api.yiku(getActivity(), mchanpin, rukuBeen.getCangkubianhao(), rukuBeen.getCangwei(), mkufang, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    RukuInfo rukuInfo= (RukuInfo) rspData;
                    RukuBeen rukuBeen=rukuInfo.getInfo();
                    List<RukuBeen> rukuBeens = homeAdapter.getData();
                    for (RukuBeen rukuBeent : rukuBeens) {
                        if (rukuBeent.getCptiaoma().equals(mchanpin)) {
                            tvYkcw.setText("");
                            tvDycw.setText("");
                            tvCptm.setText("");
                            tvCpmc.setText("");
                            //todo server OK
                            chanpin = "";
                            kufang = "";
                            ToastUtils.show("条码已移库 ");
                            return;
                        }
                    }
                    homeAdapter.addData(0, rukuBeen);
                    homeAdapter.notifyItemChanged(0);
                    rvList.smoothScrollToPosition(0);
                    tvYkcw.setText("");
                    tvCptm.setText("");
                    tvCpmc.setText("");
                    tvDycw.setText("");
                    //todo server OK
                    chanpin = "";
                    kufang = "";
                }else{
                    ToastUtils.show(rspData.getMsg());
                    tvYkcw.setText("");
                    kufang = "";
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
                tvYkcw.setText("");
                kufang = "";
            }

            @Override
            public void onUsedCache(String msg) {

            }
        },RukuInfo.class);
    }

    private void initAdapter() {

        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(0.5f).setSpaceColor(getResources().getColor(R.color.item_press2)));
        homeAdapter = new RukuAdapter();
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_name:
                        break;
                }
            }
        });
      /*  homeAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                final RukuBeen rukuBeen = (RukuBeen) adapter.getData().get(position);
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        .setTitle("是否删除产品编号" + rukuBeen.getCptiaoma() + "这条记录")
                        .setNegativeButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doDelete(rukuBeen, position);
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
                return false;
            }
        });*/
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {

            }
        });
        homeAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvList.getParent());
        rvList.setAdapter(homeAdapter);

        homeAdapter.setNewData(null);
    }

    private void doDelete(RukuBeen rukuBeen, int postion) {
        homeAdapter.remove(postion);
    }

    private void initView() {
        tvCpmc.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvCpmc.setHorizontallyScrolling(true);
        etKucun.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                etKucun.setInputType(InputType.TYPE_CLASS_TEXT);
                return false;
            }
        });
        etKucun.setInputType(InputType.TYPE_NULL);
    }
}
